﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using StackExchange.Redis;
using Microsoft.AspNetCore.Mvc;
using Books.Orders;

namespace Books.Gouwu
{
    public class GouAppService: CrudAppService<Gou,GouDto,int, PagedAndSortedResultRequestDto, CreateUpdateGouDto>,IGouAppService
    {
        public GouAppService(IRepository<Gou, int> repository)
            : base(repository)
        {

        }
        [HttpPost("api/app/Add")]
        public int Add(RedisDto dto)
        {
            //商品数量默认为1
            dto.num = 1;
            //为空时list添加信息报错
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接受信息
            List<RedisDto> ls1 = new List<RedisDto>();
            //获取原信息
            var list = redis.Get<List<RedisDto>>("dto1");
            if (list!=null)
            {
                ls1 = redis.Get<List<RedisDto>>("dto1");
            }
            //定义标记
            int aa = 0;
            foreach (var item in ls1)
            {
                if (item.id==dto.id)
                {
                    item.num += dto.num;
                    aa += 1;
                }
            }
            if (aa==0)
            {
                ls1.Add(dto);
            }
            //添加
            redis.Set("dto1", ls1);
            return 1;//返回信息
        }
        //显示购物车信息
        [HttpGet("api/app/ShowRedis")]
        public List<RedisDto> ShowRedis()
        {
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDto> ls1 = new List<RedisDto>();
            ls1 = redis.Get<List<RedisDto>>("dto1");
            return ls1;//返回信息
        }
        //商品数量相加
        [HttpPost("api/app/Addnum")]
        public int Addnum(int id)
        {
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDto> l2 = new List<RedisDto>();
            l2 = redis.Get<List<RedisDto>>("dto1");
            //循环遍历
            foreach (var item in l2)
            {
                if (item.id==id)
                {
                    item.num += 1;
                    break;
                }
            }
            //信息存回redis
            redis.Set("dto1", l2);
            return 1;
        }
        //商品数量相减
        [HttpPost("api/app/Jiannum")]
        public int Jiannum(int id)
        {
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDto> l2 = new List<RedisDto>();
            l2 = redis.Get<List<RedisDto>>("dto1");
            //循环遍历
            foreach (var item in l2)
            {
                if (item.id == id)
                {
                    item.num -= 1;
                    break;
                }
            }
            //信息存回redis
            redis.Set("dto1", l2);
            return 1;
        }
        //删除（单个）
        [HttpPost("api/app/Del")]
        public int Del(int id)
        {
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDto> l2 = new List<RedisDto>();
            l2 = redis.Get<List<RedisDto>>("dto1");
            foreach (var item in l2)
            {
                if (item.id==id)
                {
                    var t = item;
                    l2.Remove(t);
                    break;
                }
            }

            //信息存回redis
            redis.Set("dto1", l2);
            return 1;

        }
        //批量删除
        [HttpPost("api/app/DelAll")]
        public int DelAll(string id)
        {
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDto> l2 = new List<RedisDto>();
            List<RedisDto> l3 = new List<RedisDto>();
            l2 = redis.Get<List<RedisDto>>("dto1");
            //字符串分割为数组
            string[] strt = id.Split(",");
            int[] intty = Array.ConvertAll<string, int>(strt, x => int.Parse(x));
            //批量删除信息
            foreach (var item in intty)
            {
                foreach (var item1 in l2)
                {
                    if (item==item1.id)
                    {
                        l3.Add(item1);
                        continue;
                    }
                }
            }
            //删除信息
            foreach (var item in l3)
            {
                l2.Remove(item);
            }
            //信息存回redis
            redis.Set("dto1", l2);
            return 1;
        }
    }
}
